package fr.idak.spring.tiles.web.security;

import java.util.Collection;

import org.springframework.ldap.core.DirContextAdapter;
import org.springframework.ldap.core.DirContextOperations;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.ldap.userdetails.UserDetailsContextMapper;


public class CustomUserDetailsContextMapper implements UserDetailsContextMapper{

	//private UserService userService; 
	
	@Override
	public UserDetails mapUserFromContext(DirContextOperations ctx,
			String username, Collection<? extends GrantedAuthority> authorities) {
		
		String fullname = ctx.getStringAttribute("cn");
		String email    = ctx.getStringAttribute("mail");
		
		//TODO
		//authorities = userService.findAuthoritiesByUsername(username);
		
		final CustomUserDetails user = new CustomUserDetails(username, "", true, true, true, true, authorities, fullname, email);		
		return  user;
	}

	@Override
	public void mapUserToContext(UserDetails userDetails, DirContextAdapter ctx) {
	}

}
